// pages/foodManagement/foodManagement.js
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    FoodListDowm:[],
    FoodListUp:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
this.getFoodList()
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getFoodList()

  },
getFoodList(){
  const id = wx.getStorageSync('shopid')
  wx.request({
    url: `http://localhost:5555/shop/getShopAll?shopid=${id}`,
    method:"get",
    success:(res)=>{
      let FoodListDowm = [];
      let FoodListUp = [];
      let data = res.data;
      for(let i =0;i<data.length;i++){
        if(data[i].status==0){
          FoodListDowm.push(data[i]);
        }else if(data[i].status==1){
          FoodListUp.push(data[i])
        }
      }
      this.setData({
        FoodListDowm,
        FoodListUp
      })
    }
  })
},
changeStatu(e){
  console.log(e.currentTarget.dataset);
  let foodid = e.currentTarget.dataset.foodid;
  let status = e.currentTarget.dataset.status;
  wx.showLoading({
    title:`${status}ing`
  })
  new Promise((resolve)=>{
    wx.request({
      url: `http://localhost:5555/shop/FoodStatusChange?id=${foodid}&status=${status}`,
      method:"get",
      success(res){
        resolve(res)
      }
    })
  }).then((res)=>{
    this.getFoodList()
    wx.hideLoading()
    Toast.success(`${status==0?'下架':'上架'}提交成功`);
  })
}
})